package com.gift.service.Impl;


import com.gift.common.PaGing;
import com.gift.common.ServerResponse;
import com.gift.dao.BankCardMapper;
import com.gift.dao.GamesMapper;
import com.gift.pojo.BankCard;
import com.gift.service.BankCardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/7.
 */
@Service("bankCardService")
public class BankCardServiceImpl implements BankCardService {

    @Resource
    BankCardMapper bankCardMapper;
    @Resource
    GamesMapper gamesMapper;

    //得到所有信息
    @Override
    public PaGing getAllBypr(int page, int rows) {
        int raa = (page-1) * rows;
        int total =bankCardMapper.getCountAll();
        List<BankCard> gamesList= bankCardMapper.getAllBypr(raa,rows);
        PaGing paGing= new PaGing(total,gamesList);
        return paGing;
    }

    //新增一张银行卡
    @Override
    public ServerResponse insetNewGame(BankCard newBankCard) {
//        String  GName =gamesMapper.selectByPrimaryKey(newBankCard.getThegameid()).getGname();
//        newBankCard.setThegame(GName);
        int res = bankCardMapper.insert(newBankCard);
        if(res !=1){
            ServerResponse.createByErrorMessage("新增银行卡失败");
        }
        return ServerResponse.createBySuccessMessage("新增成功");
    }

    //修改银行卡
    @Override
    public ServerResponse updataGame(BankCard updataBankCard) {
//        String  GName =gamesMapper.selectByPrimaryKey(updataBankCard.getThegameid()).getGname();
//        updataBankCard.setThegame(GName);
        int res =bankCardMapper.updateByPrimaryKeySelective(updataBankCard);
        if(res !=1){
            ServerResponse.createByErrorMessage("修改银行卡失败");
        }
        return ServerResponse.createBySuccessMessage("修改成功");
    }

    //删除银行卡
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ServerResponse deleteGidList(ArrayList deleteIdList) {

        int res =bankCardMapper.deleteByPrimaryKeyList(deleteIdList);
        if(res <deleteIdList.size()){
            ServerResponse.createByErrorMessage("删除银行卡失败");
        }
        return ServerResponse.createBySuccessMessage("删除成功");
    }

    @Override
    public BankCard getbankCardByBid(String bid) {

        BankCard bankCard= bankCardMapper.selectByPrimaryKey(Integer.valueOf(bid));
        return bankCard;
    }

    @Override
    public List<BankCard> getAll() {
        return bankCardMapper.getAll();
    }
}
